package com.jqb.hibernatebbs.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jqb.hibernatebbs.pojo.Bbsuser;
import com.jqb.hibernatebbs.service.UserService;
import com.jqb.hibernatebbs.service.impl.UserServiceImpl;
import com.jqb.hibernatebbs.util.LittleCat;

public class UserServlet extends HttpServlet{

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req,resp);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Bbsuser user = new Bbsuser();
		String name = request.getParameter("name");
		String password = request.getParameter("password");
		if(password!=null&&name!=null&&!"".equals(name)&&!"".equals(password)){
			UserService userService = new UserServiceImpl();
			user.setName(name);
			//MD5加密
			user.setPassword(LittleCat.MD5(password));
			user = userService.checkLogin(user);
			if(user!=null){
				request.getSession().setAttribute("user", user);
				response.sendRedirect("/HibernateBBSBeta/carriageServlet");
			}else{
				request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response);
			}
		}else{
			request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response);
		}
	}
}
